package com.flyxteam.commons.dao.mybatis.api.condition;

import java.util.ArrayList;
import java.util.List;

import com.flyxteam.commons.dao.mybatis.api.exception.MybatisArgumentException;
import com.flyxteam.commons.dao.mybatis.core.constants.ConditionSign;
import com.flyxteam.commons.dao.mybatis.core.constants.LogicSign;

/**
 * 条件构建器
 * 
 * @author yuzh
 *
 */
public class ConditionBuilder {

	private List<Condition> conditionList = new ArrayList<Condition>(5);

	public List<Condition> getConditionList() {
		return conditionList;
	}

	/**
	 * 拼装查询条件
	 * 
	 * @param columnName
	 *            字段名
	 * @param conditionSign
	 *            条件符号，该方法适用于EQ | GT | GTE | LT | LTE | LIKE | LIKE_PRE |
	 *            LIKE_FIX | IS_NULL | IS_NOT_NULL
	 * @param columnSingleValue
	 *            字段值
	 * @param logicSign
	 *            条件逻辑符号
	 * @return
	 */
	public ConditionBuilder append(String columnName, ConditionSign conditionSign, Object columnSingleValue,
			LogicSign logicSign) {
		if (columnSingleValue instanceof List) {
			throw new MybatisArgumentException("condition column value must be Object");
		}

		Condition condition = new Condition(columnName, conditionSign, columnSingleValue, logicSign);

		return append(condition);
	}
	
	/**
	 * 拼装and查询条件
	 * @param columnName 字段名
	 * @param conditionSign 条件符号，该方法适用于EQ | GT | GTE | LT | LTE | LIKE | LIKE_PRE | LIKE_FIX | IS_NULL | IS_NOT_NULL
	 * @param columnSingleValue 字段值
	 * @return
	 */
	public ConditionBuilder and(String columnName, ConditionSign conditionSign, Object columnSingleValue) {
		return append(columnName, conditionSign, columnSingleValue, LogicSign.AND);
	}
	
	/**
	 * 拼装or查询条件
	 * @param columnName 字段名
	 * @param conditionSign 条件符号，该方法适用于EQ | GT | GTE | LT | LTE | LIKE | LIKE_PRE | LIKE_FIX | IS_NULL | IS_NOT_NULL
	 * @param columnSingleValue 字段值
	 * @return
	 */
	public ConditionBuilder or(String columnName, ConditionSign conditionSign, Object columnSingleValue) {
		return append(columnName, conditionSign, columnSingleValue, LogicSign.OR);
	}

	/**
	 * 拼装查询条件
	 * 
	 * @param columnName
	 *            字段名
	 * @param conditionSign
	 *            条件符号，该方法适用于BETWEEN | IN
	 * @param columnMultiValue
	 *            字段值集合
	 * @param logicSign
	 *            条件逻辑符号
	 * @return
	 */
	public ConditionBuilder append(String columnName, ConditionSign conditionSign, List<Object> columnMultiValue,
			LogicSign logicSign) {
		if (conditionSign == ConditionSign.BETWEEN && columnMultiValue.size() != 2) {
			throw new MybatisArgumentException("between column value length must be 2");
		}
		Condition condition = new Condition(columnName, conditionSign, columnMultiValue, logicSign);

		return append(condition);
	}
	
	/**
	 * 拼装and查询条件
	 * 
	 * @param columnName
	 *            字段名
	 * @param conditionSign
	 *            条件符号，该方法适用于BETWEEN | IN
	 * @param columnMultiValue
	 *            字段值集合
	 * @return
	 */
	public ConditionBuilder and(String columnName, ConditionSign conditionSign, List<Object> columnMultiValue) {
		return append(columnName, conditionSign, columnMultiValue, LogicSign.AND);
	}
	
	/**
	 * 拼装or查询条件
	 * 
	 * @param columnName
	 *            字段名
	 * @param conditionSign
	 *            条件符号，该方法适用于BETWEEN | IN
	 * @param columnMultiValue
	 *            字段值集合
	 * @return
	 */
	public ConditionBuilder or(String columnName, ConditionSign conditionSign, List<Object> columnMultiValue) {
		return append(columnName, conditionSign, columnMultiValue, LogicSign.OR);
	}

	/**
	 * 拼装查询条件
	 * 
	 * @param condition
	 *            查询条件实体对象
	 * @return
	 */
	private ConditionBuilder append(Condition condition) {
		conditionList.add(condition);

		return this;
	}

}
